package com.chaodev.base.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class FineDigitalSignature {
    //数字签名密钥，可以自定义
    public static final String key = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";
    //数字签名有效时长
    public static final long validTime = 30 * 60 * 1000;

    public static String createToken(String issuer, String id, String subject) throws Exception {
        Date currentTime = new Date();
        Date expirationTime = new Date(currentTime.getTime() + validTime);

        Map<String, Object> map = new HashMap<>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String token = JWT.create()
                .withHeader(map)//头
                .withIssuer(issuer)
                .withSubject(subject)//
                .withIssuedAt(new Date())//签名时间
                .withExpiresAt(expirationTime)//过期时间
                .withJWTId(id)
                .sign(Algorithm.HMAC256(key));//签名
        return token;
    }

}
